package com.wl.lib.mvi

import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

/*
 * BaseFragment (vm + viewBinding)
 *
 * Created by wangliang on 2024/4/25.
 */
abstract class BaseBindingVmFragment<VB : ViewBinding, VM : BaseViewModel<out IUiIntent, out IUiEvent>> :
    BaseBindingFragment<VB>() {

    protected val vm: VM by lazy { createViewModel() }

    /**
     * 创建viewModel
     */
    private fun createViewModel(): VM {
        val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
        val vmClass = type[1] as Class<VM>
        return ViewModelProvider(this) [vmClass]
    }
}